package com.lagou.product.controller;

import com.lagou.common.pojo.Products;
import com.lagou.common.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @SuppressWarnings("Spring")
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @GetMapping("/query/{id}")
    public Products queryById(@PathVariable Integer id){
        return productService.queryById(id);
    }

    @PostMapping("/add")
    public Long addProduct(@RequestBody Products products){
        //插入商品
        productService.addProduct(products);

        //将该id作为消息发送到Kafka的product主题中
        ListenableFuture<SendResult<String, String>> listenableFuture = kafkaTemplate.send("product", products.getId()+"");

        return products.getId();
    }
}
